package com.atguigu.rabbitmq.simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //主机地址;默认为localhost
        factory.setHost("localhost");
        //连接端口，默认为5672
        factory.setPort(5672);
        //虚拟主机名称，默认为/
        factory.setVirtualHost("/");
        //连接用户，默认为guest
        factory.setUsername("guest");
        //连接密码。默认为guest
        factory.setPassword("guest");
        //创建连接
        Connection connection = factory.newConnection();
        //获取信道，信道是用来发消息的
        Channel channel = connection.createChannel();

        channel.queueDeclare("simple_queue", true, false, false, null);

        //匿名内部类
        DefaultConsumer consumer = new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumerTag：" + consumerTag);
                System.out.println("Exchange：" + envelope.getExchange());
                System.out.println("RoutingKey：" + envelope.getRoutingKey());
                System.out.println("properties：" + properties);
                System.out.println("body：" + new String(body));
            }
        };




        channel.basicConsume("simple_queue", true, consumer);

    }
}
